package apiwork.lcbmi.interceptors;

import java.util.Map;

import org.apache.log4j.Logger;


import apiwork.lcbmi.protocaladaptor.DefaultProtocolAdaptor;
import apiwork.lcbmi.protocaladaptor.ProtocolAdaptor;
import bsh.This;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
import com.opensymphony.xwork2.util.ValueStack;

@SuppressWarnings("serial")
public class ProtocolAnalyseInterceptor extends AbstractInterceptor {
	private final Logger log=Logger.getLogger(ProtocolAnalyseInterceptor.class);//zhaigx
	private final String PROTOCAL_ANALYSE_ERROR = "protocal_analyse_error";

	private String adaptor = null;

	@Override
	public String intercept(ActionInvocation invocation) throws Exception {
		log.debug("interceptor........");
		ValueStack stack = invocation.getStack();
		Map paramsMap = (Map) stack.getContext().get(ActionContext.PARAMETERS);
		Object datamessage = paramsMap.get("datamessage");
		log.debug("datamessage in interceptor:" + datamessage);
		if (null == adaptor)
			adaptor = DefaultProtocolAdaptor.class.getName();
		ProtocolAdaptor protocolAdaptor = (ProtocolAdaptor) Class.forName(
				adaptor).newInstance();
		boolean analyseResult = false;
		try {
			analyseResult = protocolAdaptor.protocolAnalyse(datamessage, paramsMap);
		}catch(Exception e){
			return PROTOCAL_ANALYSE_ERROR;
		}
		if (analyseResult) {
			return invocation.invoke();
		} else {
			return PROTOCAL_ANALYSE_ERROR;
		}
		
	}

	public String getAdaptor() {
		return adaptor;
	}

	public void setAdaptor(String adaptor) {
		this.adaptor = adaptor;
	}

}
